//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package mutsumimod.actions;

import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.vfx.SpeechBubble;
import mutsumimod.cards.BaseCard;
import mutsumimod.cards.derivative.CurseImagination;
import mutsumimod.cards.custom.KeepSecret;
import mutsumimod.patches.CardRewardScreenPatch;
import mutsumimod.patches.CopyPatch;

import java.util.ArrayList;

public class CopyChooseOneAction extends AbstractGameAction {
    public ArrayList<AbstractCard> choices;
    public boolean canCopy=true;

    public CopyChooseOneAction(AbstractCard card) {
        this.duration = Settings.ACTION_DUR_FAST;

//        if(card instanceof BaseCard) {
//            ((BaseCard) card).beforeCopy();
//        }

        this.choices=new ArrayList<>();
        for(AbstractCard c:CopyPatch.AbstractCardFieldPatch.copyList.get(card)){
            AbstractCard copyCard=c.makeStatEquivalentCopy();
            CopyPatch.AbstractCardFieldPatch.originCard.set(copyCard, c);
            this.choices.add(copyCard);
        }

        if(this.choices.isEmpty()){
            canCopy=false;
            return;
        }

        if(!containsCard(KeepSecret.ID)){
            if(getFirstCard(KeepSecret.ID)!=null){
                AbstractCard originCard=getFirstCard(KeepSecret.ID);
                AbstractCard keepSecret=originCard.makeStatEquivalentCopy();
                CopyPatch.AbstractCardFieldPatch.originCard.set(keepSecret,originCard);
                int randonTarget=AbstractDungeon.cardRng.random(0,choices.size()-1);
                this.choices.set(randonTarget, keepSecret);
            }
        }

        if(getFirstCard(CurseImagination.ID)!=null){
            AbstractCard originCurse=getFirstCard(CurseImagination.ID);
            for(int i=0;i<this.choices.size();i++){
                AbstractCard curseImagination=originCurse.makeStatEquivalentCopy();
                CopyPatch.AbstractCardFieldPatch.originCard.set(curseImagination,originCurse);
                this.choices.set(i, curseImagination);
            }
        }

        for(AbstractCard c : this.choices) {
            CopyPatch.AbstractCardFieldPatch.inCopyUse.set(c, true);
            c.triggerOnGlowCheck();
            c.isGlowing=c.canUse(AbstractDungeon.player,null);
            c.purgeOnUse = true;
            c.applyPowers();
            if(c instanceof BaseCard){
                ((BaseCard) c).postCopyBeforeUse(choices);
                ((BaseCard) c).updateDescription();
            }
        }
    }

    public AbstractCard getFirstCard(String cardID){
        for(AbstractCard c:AbstractDungeon.player.hand.group){
            if(c.cardID.equals(cardID)){
                return c;
            }
        }
        return null;
    }

    public boolean containsCard(String cardID){
        for(AbstractCard c:choices){
            if(c.cardID.equals(cardID)){
                return true;
            }
        }

        return false;
    }

    public void update() {
        if(!canCopy){
            AbstractDungeon.effectList.add(new SpeechBubble(AbstractDungeon.player.dialogX,
                    AbstractDungeon.player.dialogY, 1.0f, "没有可以模仿的卡牌", true));
            isDone = true;
            return;
        }
        if (this.duration == Settings.ACTION_DUR_FAST) {
            if(checkAllCantUse()){
                AbstractDungeon.effectList.add(new SpeechBubble(AbstractDungeon.player.dialogX,
                        AbstractDungeon.player.dialogY, 1.0f, "所有卡牌均不能打出", true));
                isDone = true;
                return;
            }
            CardRewardScreenPatch.CardRewardScreenFieldPatch.isCopyChooseOne.set(AbstractDungeon.cardRewardScreen,true);
            AbstractDungeon.cardRewardScreen.chooseOneOpen(this.choices);
            this.tickDuration();
        } else {
            this.tickDuration();
        }
    }

    public boolean checkAllCantUse(){
        for(AbstractCard c:this.choices){
            if(c.canUse(AbstractDungeon.player,null)){
                return false;
            }
        }
        return true;
    }
}
